function Q1() {
    var k = parseInt(prompt("输入一个数"));
    var result = 0;
    for (var i = k ;i >= 1; i--) {

        var t = i;
        var temp = 1;
        while(t) {
            temp*=t;
            t--;
        }
        result = result + temp;
        temp = 0;
    }
    console.log("阶乘和为：" + result);
}

function Q2() {
    for(var i=100; i <= 999;i++) {
        var sum = 0;
        var result = 1;
        var result1 = 1;
        var result2 = 1;
        var ge = i % 10
        var shi = parseInt(i / 10) % 10;
        var bai = parseInt(i / 100);
        while(ge){
            result*=ge;
            ge--;
        }
        while(shi){
            result1*=shi;
            shi--;
        }
        while(bai){
            result2*=bai;
            bai--;
        }
        sum = result + result1 + result2
        if (sum == i) {
            console.log(i)
        }
    }
}


function Q3() {
    while (true){
        var k = parseInt(prompt("输入一个1~1000之间的偶数"));
        if (k <= 1000 && k >= 0 && k % 2 ==0){
            break;
        } else {
            continue;
        }
    }
    var temp = 0;
    var k1 = 0;
    var k2 = 0;
    for (var m = 1; m <=k;m++){
        if(zhishu(m)){
            temp = k - m;
            if(zhishu(temp)){
                k1 = m;
                k2 = temp;
            }
        }
    }
    console.log("数字" + k + "由" + k1 + "和" + k2 + "组成")
}

function zhishu(k){
    var n = 0;
    for(var i=1;i<=k;i++) {
        if (k % i ==0) {
            n++;
        }
    }
    if (n == 2) {
        return true;
    }else {
        return false;
    }
}

function Q4() {
    var i = prompt("输入一串文字");
    var str = i.split("")
    var result = str.reverse().join("")
    console.log("字符串" + i + "的倒置结果为" + result)
}

function Q5() {
    var str = "i come from beijing"
    var temp = str.split(" ");
    var result = new Array();
    var k = 0;
    for (var i = temp.length; i >=0; i--){
        result[k] = temp[i];
        k++;
    }
    result = result.join(" ")
    console.log(result)
}

function Q6() {
    var temp = new Array(33,44,55,66,77,88,31,46,74,89);
    var k = eval(temp.join("+"));
    var avg = k / 10;
    var result = 0;
    for (var i = 0; i < temp.length; i++){
        if (temp[i] >= avg){
            result++;
        }
    }
    console.log("成绩大于等于平均成绩的人数为", + result);
}

function Q7() {
    var str = "i love javascript very much";
    var temp = str.split(" ");
    var temp1 = "";
    var result = "";
    temp.forEach(function(m){
        let s1 = m.slice(0,1).toUpperCase();
        let s2 = m.slice(1)
        temp1 = s1 + s2 + " ";
        result += temp1;
    })
    console.log(result.substring(0,result.length-1))
}

function Q8() {
    var temp = [1,44,55,66,77,88,31,46,74,89];
    console.log("数组[" + temp + "]中最大值及其下标为" + Math.max(...temp) + "," + temp.indexOf(Math.max(...temp)) + ",最小值及其下标为" + Math.min(...temp) + "," + temp.indexOf(Math.min(...temp)) )
}

function Q9() {
    var temp = ["1","44","haha","55","66","77","lagou","88","31","46","74","89"];
    var result = temp.join("|")
    console.log(result);
}

function Q10() {
    var temp = ["1","0","haha","55","66","77","lagou","0","31","0","74","89"];
    var result = [];
    temp.forEach(function(m){
        if(m !== "0") {
            result.push(m)
        }
    })
    console.log(result);
}

function Q11() {
    var temp = [1,1,1,2,2,3,4,5,6,3,4,2,4,1,];
    var result = [];
    for (var i = 0; i < temp.length; i++){
        if (!result.includes(temp[i])){
            result.push(temp[i])
        }
    }
    console.log(result);
}

function Q12(){
    var temp = [3,4,1,2,6,5];
    var result = temp.sort(function(a,b){console.log(a+"kk"+b); return a-b});
    console.log(result);
}

function Q13() {
    var temp =  ['a','a','a','b','b','c','c','c','c','d','d','d','d'];
    var result = new Object();
    for(var value of temp)
    {
        if(result[value]){
            result[value]++;
        }else{
            result[value]=1;
        }
    }
    console.log(result);
}

function Q14() {
    var first = 9;
    var second = 9;
    var sum = 0;
    for(var i = 1;i <= first;i++) {
        for(var j = 1;j <= second;j++) {
            sum = i * j;
            console.log(i + "x" + j + "=" + sum);
        }
    }
}

function Q15() {
    var k = [7, 1, 5, 3, 6, 4];
    var temp = [];
    var result = 0;
    for(var i = 0;i < k.length;i++) {
        for(var j = 0;j <= i;j++) {
            temp.push(k[i]-k[j]);
        }
    }
    result = Math.max(...temp)
    console.log(result);
}
